import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-condition',
    templateUrl: './condition.component.html',
    styleUrls: ['./tectonicSynthesis.component.less']
})
export class ConditionComponent implements OnInit {
    @Input() pageType: any;
    @Output() private searchOuter = new EventEmitter();
    param: any = {};
    czSearch = [];
    wcztSelect = [{ "value": "1", "label": "完成" }, { "value": "0", "label": "未完成" }];
    stationTemp = [];
    stationData = [];

    constructor(private http: _HttpClient) { }

    ngOnInit() {
        this.stationList();
    }
    search() {
        let url = '';
        if (this.param.wczt) {
            url += '&finished=' + this.param.wczt;
        }
        if (this.param.rq) {
            url += '&analysisDate=' + this.timeToDate(this.param.rq);
        }
        if (this.param.xb) {
            url += '&line=' + this.param.xb;
        }
        if (this.param.cz) {
            url += '&startStation=' + this.param.cz;
        }
        if (this.param.cc) {
            url += '&trainNo=' + this.param.cc;
        }
        if (this.param.ch) {
            url += '&trainNum=' + this.param.ch;
        }
        if (this.param.fxr) {
            url += '&analysisUser=' + this.param.fxr;
        }
        this.searchOuter.emit(url);
    }
    stationInput(e) {
        const value = e.target.value;
        switch (value) {
            case '':
            case null:
            case undefined:
                this.stationData = [];
                break;
            default:
                this.stationData = this.stationTemp.filter((option) => {
                    if (option.indexOf(value) !== -1) {
                        return option;
                    }
                });
                break;
        }
    }
    stationList() {
        const url = environment.apiBase + '/csmis/publicTree/getorgstatree.jwt';
        this.http.get(url).subscribe((res: any) => {
            this.childToList(this.stationTemp, res);
        });
    }
    // children转list
    childToList(array, list) {
        list.forEach(item => {
            array.push(item.text);
            if (item.children && item.children.length) {
                this.childToList(array, item.children);
            }
        })
    }
    timeToDate(time) {
        const d = new Date(time);
        const YY = d.getFullYear();
        const MM = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
        const DD = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
        return YY + '-' + MM + '-' + DD;
    }
}
